# -*- coding: utf-8 -*-
# @Time    : 2019/3/7 16:07
# @Author  : z.t.b
# @Email   : zhtbin@163.com
# @File    : bitOperation.py
# @Software: PyCharm
"""
位运算
1、判断基数偶数
2、交换两个数
"""

import time


class Solution(object):

    def assertEvenOrOddWithBitoperation(self):
        even_count = 0
        odd_count = 0
        # 位操作
        startTime = time.time()
        for i in range(100000000):
            if i & 1 == 0:
                even_count += 1
            else:
                odd_count += 1
        endTime = time.time()
        print("used:", endTime-startTime)
        print("event_count:", even_count, "odd_count:", odd_count)

    def assertEvenOrOddWithCommon(self):
        # 模操作
        even_count = 0
        odd_count = 0
        startTime = time.time()
        for i in range(100000000):
            if i % 2 == 0:
                even_count += 1
            else:
                odd_count += 1
        endTime = time.time()
        print("used:", endTime-startTime)
        print("event_count:", even_count, "odd_count:", odd_count)

    # 通过位运算交换两个数
    # 原理：两个相等的数异或是0，一个数和0异或后值不变
    def swapNumber(self):
        num1 = 10
        num2 = 20
        num1 = num1 ^ num2
        num2 = num1 ^ num2
        num1 = num1 ^ num2
        print("{}-{} swap to {}-{}".format(10, 20, num1, num2))


if __name__ == "__main__":
    solution = Solution()
    solution.swapNumber()
